package io.github.karino2.pngnote.book;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.ParcelFileDescriptor;

import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.regex.Pattern;

import io.github.karino2.pngnote.Book;

public class BookIO {
    private ContentResolver resolver;
    private Pattern pageNamePat;

    public BookIO(ContentResolver resolver) {
        this.resolver = resolver;
        this.pageNamePat = Pattern.compile("([0-9][0-9][0-9][0-9])\\.png");
    }

    private Bitmap loadBitmap(FastFile file) {
        Closeable var3 = null;
        Throwable var4 = null;
        Bitmap var11 = null;
        try {
            var3 = (Closeable)this.resolver.openFileDescriptor(file.getUri(), "r");
            ParcelFileDescriptor it = (ParcelFileDescriptor)var3; //FIXME: check null
            var11 = BitmapFactory.decodeFileDescriptor(it.getFileDescriptor());
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            try {
                if (var3 != null) {
                    var3.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return var11; //FIXME: check null
    }

    private Bitmap loadThumbnail(FastFile bookDir, String displayName) {
        FastFile var3 = bookDir.findFile(displayName);
        Bitmap var10000;
        if (var3 != null) {
            FastFile it = var3;
            var10000 = this.loadBitmapThumbnail(it, 3);
        } else {
            var10000 = null;
        }

        return var10000;
    }

    public Bitmap loadThumbnail(FastFile bookDir) {
        return this.loadThumbnail(bookDir, "0000.png");
    }

    public Bitmap loadBgThumbnail(FastFile bookDir) {
        return this.loadThumbnail(bookDir, "background.png");
    }

    public Bitmap loadPageThumbnail(FastFile file) {
        return this.loadBitmapThumbnail(file, 4);
    }

    public Bitmap loadBgForGrid(FastFile bookDir) {
        FastFile var2 = bookDir.findFile("background.png");
        Bitmap var10000;
        if (var2 != null) {
            FastFile it = var2;
            var10000 = this.loadBitmapThumbnail(it, 4);
        } else {
            var10000 = null;
        }

        return var10000;
    }

    private Bitmap loadBitmapThumbnail(FastFile file, int sampleSize) {
        Closeable var4 = null;
        Bitmap var16 = null;
        try {
            var4 = (Closeable)this.resolver.openFileDescriptor(file.getUri(), "r");
            ParcelFileDescriptor it = (ParcelFileDescriptor)var4;
            BitmapFactory.Options var8 = new BitmapFactory.Options();
            BitmapFactory.Options $this$loadBitmapThumbnail_u24lambda_u244_u24lambda_u243 = var8;
            $this$loadBitmapThumbnail_u24lambda_u244_u24lambda_u243.inSampleSize = sampleSize;
            BitmapFactory.Options option = var8;
            var16 = BitmapFactory.decodeFileDescriptor(it.getFileDescriptor(), (Rect)null, option);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            try {
                if (var4 != null) {
                    var4.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return var16;
    }

    private boolean isEmpty(FastFile file) {
        return file.isEmpty();
    }

    public boolean isPageEmpty(BookPage page) {
        return this.isEmpty(page.getFile());
    }

    public Bitmap loadBitmap(BookPage page) {
        return this.loadBitmap(page.getFile());
    }

    public Bitmap loadBitmapOrNull(BookPage page) {
        return this.isPageEmpty(page) ? null : this.loadBitmap(page);
    }

    public Bitmap loadBgOrNull(Book book) {
        FastFile var2 = book.getBgImage();
        Bitmap var10000;
        if (var2 != null) {
            FastFile it = var2;
            var10000 = this.loadBitmap(it);
        } else {
            var10000 = null;
        }

        return var10000;
    }

    public void saveBitmap(BookPage page, Bitmap bitmap) {
        Closeable var3 = null;
        try {
            var3 = this.resolver.openOutputStream(page.getFile().getUri(), "wt");
            OutputStream it = (OutputStream)var3;
            Bitmap.CompressFormat var10001 = Bitmap.CompressFormat.PNG;
            bitmap.compress(var10001, 80, it);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            try {
                if (var3 != null) {
                    var3.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public Book loadBook(FastFile bookDir) {
        Map pageMap = new HashMap();

        //FIXME：??
        int var10000;
        if (pageMap.isEmpty()) {
            var10000 = 0;
        } else {
            Iterator var5 = ((Iterable)pageMap.entrySet()).iterator();
            if (!var5.hasNext()) {
                throw new NoSuchElementException();
            }

            Map.Entry it = (Map.Entry)var5.next();
            int var7 = 0;
            int var19 = ((Number)it.getKey()).intValue();

            while(var5.hasNext()) {
                it = (Map.Entry)var5.next();
                int var22 = ((Number)it.getKey()).intValue();
                if (var19 < var22) {
                    var19 = var22;
                }
            }

            var10000 = var19;
        }

        int lastPageIdx = var10000;
        List<Integer> kkk = new ArrayList<Integer>();
        for (int i = 0; i <= lastPageIdx; ++i) {
            kkk.add(i);
        }
        List $this$map$iv = (List)(kkk);
        List<FastFile> destination$iv$iv = new ArrayList(
                $this$map$iv != null ? $this$map$iv.size() : 10);
        Iterator var10 = $this$map$iv.iterator();
        while(var10.hasNext()) {
            int item$iv$iv = (int)((Iterator<Integer>)var10).next();
            int it = item$iv$iv;
            FastFile var14 = (FastFile)pageMap.get(it);
            FastFile var16 = var14 != null ? var14 : BookPage.createEmptyFile(bookDir, it);
            destination$iv$iv.add(var16);
        }

        List<FastFile> pages = (List<FastFile>)destination$iv$iv;
        FastFile bgFile = bookDir.findFile("background.png");
        return new Book(bookDir, pages, bgFile);
    }
}
